﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.IO;

namespace Volume_Resource_Management
{
    class mapManagement
    {
        public static int[,] loadMap(string storeNo, frmSplash splsh)
        {
            string fileName = System.Windows.Forms.Application.StartupPath + "\\storeMaps\\" + storeNo + ".txt";
            StreamReader reader = new StreamReader(fileName);
            int width;
            int height;

            int.TryParse(reader.ReadLine(), out width);
            int.TryParse(reader.ReadLine(), out height);

            int[,] toReturn = new int[width, height];

            for (int i = 0; i < toReturn.GetLength(0); i++)
            {
                for (int j = 0; j < toReturn.GetLength(1); j++)
                {
                    int.TryParse(reader.ReadLine(), out toReturn[i, j]);
                    splsh.progressBar1.Invoke(new Action(() => splsh.progressBar1.PerformStep()));      
                }
            }
            reader.Close();
            
            return toReturn;
        }

        public static void saveMap(int[,] storeMap, string storeNo)
        {
            string fileName = System.Windows.Forms.Application.StartupPath + "\\storeMaps\\" + storeNo + ".txt";
            StreamWriter writer = new StreamWriter(fileName);

            writer.WriteLine(storeMap.GetLength(0));
            writer.WriteLine(storeMap.GetLength(1));

            for (int i = 0; i < storeMap.GetLength(0); i++)
            {
                for (int j = 0; j < storeMap.GetLength(1); j++)
                {
                    writer.WriteLine(storeMap[i, j]);
                }
            }
            writer.Close();
        }

        public static void saveMap(int[,] storeMap, string storeNo, frmFirstLoad fLoad)
        {
            string fileName = System.Windows.Forms.Application.StartupPath + "\\storeMaps\\" + storeNo + ".txt";
            StreamWriter writer = new StreamWriter(fileName);

            writer.WriteLine(storeMap.GetLength(0));
            writer.WriteLine(storeMap.GetLength(1));

            for (int i = 0; i < storeMap.GetLength(0); i++)
            {
                for (int j = 0; j < storeMap.GetLength(1); j++)
                {
                    writer.WriteLine(storeMap[i, j]);
                    fLoad.progressBar1.Invoke(new Action(() => fLoad.progressBar1.PerformStep()));
                }
            }
            writer.Close();
        }
    }
}
